package com.mvchib.dao.impl;

import com.mvchib.dao.HpBbsDAO;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.mvchib.model.HpBbs;

@Repository
public class HpBbsDAOImpl implements HpBbsDAO {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void addBbs(HpBbs bbs) {
        getCurrentSession().save(bbs);
    }

    @Override
    public void updateBbs(HpBbs bbs) {
        HpBbs bbsToUpdate = getBbs(bbs.getBid());
        bbsToUpdate.setTitle(bbs.getTitle());
        bbsToUpdate.setKeyword(bbs.getKeyword());
        getCurrentSession().update(bbsToUpdate);

    }

    @Override
    public HpBbs getBbs(int bid) {
        HpBbs team = (HpBbs) getCurrentSession().get(HpBbs.class, bid);
        return team;
    }

    @Override
    public void deleteBbs(int bid) {
        HpBbs bbs = getBbs(bid);
        if (bbs != null) {
            getCurrentSession().delete(bbs);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<HpBbs> getBbss() {
        return getCurrentSession().createQuery("from HpBbs").list();
    }

}
